; picture.asm
; Picture demo window
;
	.include "common.inc"
	.include "global.inc"
	.include "window.inc"
	.include "graphic.inc"
	.include "drawfun.inc"
	.include "execfun.inc"

	.export picture
	.export anypic
	
	.import bltdmpic
	.import bltanypic

	.segment "RODATA"
	
tx_pic:		.byte 13, $42, $0C, $09, $0E, $0B, $05, $0E, $0C, $09, $07, $08, $14, $13
tx_any:		.byte 6, $56, $09, $03, $2D, $32, $30

	BC_WIDTH = 7
	BC_HEIGHT = 7

b_close:	.byte %01111100, %10111010, %11010110, %11101110
			.byte %11010110, %10111010, %01111100
bm_close:	.byte BC_WIDTH, BC_HEIGHT
			.word b_close
tx_close:	.byte $FF
			.word bm_close


	.segment "DATA"
	
f_pic:		.byte $00
h_pic:		.byte $00

f_any:		.byte $00
h_any:		.byte $00

bptmp:		.byte $00

	.segment "CODE"

			BM_WIDTH = 208
			BM_HEIGHT = 98

			ANY_WIDTH = 96
			ANY_HEIGHT = 160

picture:	lda f_pic
			beq @create
			jmp frm_activate
@create:	; create main frame
			ldx #BM_WIDTH
			ldy #BM_HEIGHT
			jsr frm_create
			sta f_pic
			ldx #(MAX_WIDTH - BM_WIDTH) / 2
			ldy #(MAX_HEIGHT - BM_HEIGHT) / 4
			jsr frm_setpos
			jsr frm_getwin
			sta h_pic
			jsr win_realize
			lda #WSTYLE_BCKGND+WSTYLE_HASTITLE
			jsr win_setstyle
			lda #<drw_pic
			ldx #>drw_pic
			jsr win_setdraw
			lda #<tx_pic
			ldx #>tx_pic
			jsr win_settext
			lda #1					; lower case system font
			jsr win_setfont

			lda h_pic
			jsr add_close
			lda #<ex_close_pic
			ldx #>ex_close_pic
			jsr win_setexec

			; draw window			
			lda h_pic
			jsr win_draw
			lda h_pic
			jmp win_show			; to screen

anypic:		lda f_any
			beq @create
			jmp frm_activate
@create:	; create main frame
			ldx #ANY_WIDTH
			ldy #ANY_HEIGHT
			jsr frm_create
			sta f_any
			ldx #(MAX_WIDTH - ANY_WIDTH) / 2
			ldy #(MAX_HEIGHT - ANY_HEIGHT) / 2
			jsr frm_setpos
			jsr frm_getwin
			sta h_any
			jsr win_realize
			lda #WSTYLE_BCKGND+WSTYLE_HASTITLE
			jsr win_setstyle
			lda #<drw_any
			ldx #>drw_any
			jsr win_setdraw
			lda #<tx_any
			ldx #>tx_any
			jsr win_settext
			lda #1					; lower case system font
			jsr win_setfont

			lda h_any
			jsr add_close
			lda #<ex_close_any
			ldx #>ex_close_any
			jsr win_setexec

			; draw window			
			lda h_any
			jsr win_draw
			lda h_any
			jmp win_show			; to screen

add_close:	; A .. parent window
			jsr win_create
			lda #WSTYLE_BCKGND
			jsr win_setstyle
			ldx #1
			ldy #1
			jsr win_setpos
			ldx #BC_WIDTH
			ldy #BC_HEIGHT
			jsr win_setdim
			lda #<tx_close
			ldx #>tx_close
			jsr win_settext
			lda #WTYPE_BUTTON
			jmp win_settype

ex_close_pic:
			jsr ex_button
			ldy #Event::type
			lda (reg2), y
			cmp #EVT_MOUSE_UP
			beq @close
			rts
@close:		lda f_pic
			jsr frm_destroy
			lda #0
			sta f_pic
			rts

ex_close_any:
			jsr ex_button
			ldy #Event::type
			lda (reg2), y
			cmp #EVT_MOUSE_UP
			beq @close
			rts
@close:		lda f_any
			jsr frm_destroy
			lda #0
			sta f_any
			rts

	.importzp regG

drw_pic:	jsr drw_window
			ldx #0
			ldy #11
			jsr g_moveto
			lda regG
			ldx regG+1
			jsr bltdmpic
			rts

drw_any:	jsr drw_window
			ldx #0
			ldy #20
			jsr g_moveto
			lda regG
			ldx regG+1
			jsr bltanypic
			rts
